﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FantastPlatform.Community.IMode;
using FantastPlatform.Framework.Core;
using FantastPlatform.Community.IService;
using FantastPlatform.Framework;
using FantastPlatform.Framework.IModel;

namespace FantastPlatform.Community.UI.Service
{
    public partial class PersonRelationshipService : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoginEmployee = Session["User"] as FantastPlatform.Framework.Model.Employee;
            IPersonRelationTypeService prtService = ServiceFactory.CreateService<IPersonRelationTypeService>();
            ConditionGroup group = new ConditionGroup();
            group.Conditions.Add(new Condition { Field = "Enable", Type = typeof(bool).FullName, Value1 = true });
            int totalCount = 0;
            switch (Action)
            {
                case "Add":
                    //if (LoginEmployee != null)
                    //{
                    //    if (LoginEmployee.User != null)
                    //    {
                    //        IList<IRole> roleList = LoginEmployee.User.Roles;
                    //        List<string> roles = new List<string>();
                    //        foreach (IRole role in roleList)
                    //        {
                    //            roles.Add(role.Name);
                    //        }
                    //        if (roles.Contains("社区一级管理员") || roles.Contains("管理员"))
                    //        {
                    string data = Data;
                    PersonRelationView view = JsonToEntity<PersonRelationView>(RepairString(data));
                    IPersonRelationType ps = new FantastPlatform.Community.Model.PersonRelationType();
                    if (view.Id == 0)
                    {
                        ps.Name = view.Name;
                        ps.Description = view.Description;
                        ps.CreateDate = DateTime.Now;
                        ps.Enable = true;
                        prtService.Create(ps);
                        ReturnSuccessMessage("添加状态成功");
                    }
                    else
                    {
                        ps = prtService.Get(view.Id);
                        ps.Name = view.Name;
                        ps.Description = view.Description;
                        ps.ModifyDate = DateTime.Now;
                        prtService.Update(ps);
                        ReturnSuccessMessage("修改状态成功");
                    }
                    //        }
                    //        else
                    //        {
                    //            ReturnSuccessMessage("您没有修改的权限");
                    //        }
                    //    }
                    //}

                    break;
                case "Search":
                    IList<IPersonRelationType> list = prtService.Search(group, null, PageSize, SelfPage, out totalCount);
                    object[] objArry = (from o in list
                                        select new
                                        {
                                            Id = o.Id,
                                            Name = o.Name,
                                            CreateDate = o.CreateDate.ToString(),
                                            Description = o.Description
                                        }
                                            ).ToArray();
                    ReturnList(totalCount, objArry);
                    break;
                case "Get":
                    IPersonRelationType personrelation = prtService.Get(Id);
                    ReturnObj(personrelation);
                    break;
                case "Delete":
                    if (string.IsNullOrEmpty(Data))
                    {
                        throw new Exception("传入的ID为空");
                    }
                    List<int> idList = new List<int>();
                    string[] idStrList = Data.Split(',');
                    foreach (string id in idStrList)
                    {
                        idList.Add(int.Parse(id));
                    }
                    prtService.Delete(idList);
                    ReturnSuccessMessage("删除成功");
                    break;
            }
        }
        public class PersonRelationView
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Description { get; set; }
        }
    }
}